﻿Imports System.Collections.ObjectModel
Imports FL_8x0.Common
Imports FL_8x0.FL_830.Model
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.CommandWpf

Namespace FL_8x0.FL_830.ViewModel

    Public Class ProductViewModel : Inherits ViewModelBase

        Private _productModel As ProductModel                            '产品（组参数）

        Private _idList As ObservableCollection(Of String)               '产品Id列表
        Private _fittingModeList As ObservableCollection(Of String)      'Fitting Mode模式列表

        Private _gpUnit As String                                        '直压单位
        Private _leakUnit As String                                      '小漏单位

        Private _gpUnitList As ObservableCollection(Of String)           '直压单位列表
        Private _leakUnitList As ObservableCollection(Of String)         '小漏单位列表

        Private _addCommand As RelayCommand                              '点击“新增”触发命令
        Private _editCommand As RelayCommand                             '点击“新增”触发命令

        Private _applyCommand As RelayCommand                            '点击“确认”触发命令
        Private _cancelCommand As RelayCommand                           '点击“取消”触发命令
        Private _isAdd As Boolean                                        '当前模式 


        Private _reason As String                                        '修改原因
        Private _enableSave As Boolean                                   '确认按钮状态

        Private _id As String                                            '当前ID

        Public Sub New()
            GpUnitList = New ObservableCollection(Of String)(New List(Of String)(g_strGPUnit))
            LeakUnitList = New ObservableCollection(Of String)(New List(Of String)(g_strLeakUnit))
            IdList = New ObservableCollection(Of String)
            FittingModeList = New ObservableCollection(Of String)(New List(Of String)(g_strFittingMode))
            FillAddModel()
            AddCommand = New RelayCommand(Sub() FillAddModel())
            EditCommand = New RelayCommand(Sub() Edit())
            ApplyCommand = New RelayCommand(Sub() Apply())
            CancelCommand = New RelayCommand(Sub() Cancel())

        End Sub

        '=====================================================================
        '函 数 名: Cancel
        '参    数: 
        '返 回 值:
        '功能描述: 取消重新填充ProductModel
        '=====================================================================
        Private Sub Cancel()
            If IsAdd Then
                FillAddModel()
            Else
                FillEditModel(Id)
            End If
        End Sub

        '=====================================================================
        '函 数 名: Edit
        '参    数: 
        '返 回 值:
        '功能描述: 点击编辑按钮
        '=====================================================================
        Private Sub Edit()
            IdList.Clear()
            Dim ids = User_ListProduct()

            If ids.Count > 0 Then
                For Each strId As String In ids
                    IdList.Add(strId)
                Next
                Id = ids(0)
                FillEditModel(ids(0))
            Else
                MsgBox("当前没有产品，请先添加", MsgBoxStyle.OkOnly, "错误")
                FillAddModel()
            End If
        End Sub

        '=====================================================================
        '函 数 名: CheckInput
        '参    数: 
        '返 回 值:
        '功能描述: 确认输入有效性
        '=====================================================================
        Private Function CheckInput() As Boolean

            If String.IsNullOrEmpty(Reason) Then
                log.WriteLogError("输入参数检查", "请填写修改原因！")
                Return False
            End If

            For i = 0 To ProductModel.Times.Count - 1
                If i = 2 Or i = 4 Or i = 5 Or i = 8 Then
                    If Not CheckTextNumber(Mid(g_strResultTitle(24 + i), 4), ProductModel.Times(i), 99999, 1) Then
                        Return False
                    End If
                ElseIf i = 10 Then
                    If ProductModel.FittingMode <> 4 Then
                        If Not CheckTextNumber(Mid(g_strResultTitle(24 + i), 4), ProductModel.Times(i), 99999, ProductModel.Times(5)) Then
                            Return False
                        End If
                    Else
                        If Not CheckTextNumber(Mid(g_strResultTitle(24 + i), 4), ProductModel.Times(i), 99999, 0) Then
                            Return False
                        End If
                    End If

                Else
                    If Not CheckTextNumber(Mid(g_strResultTitle(24 + i), 4), ProductModel.Times(i), 99999, 0) Then
                        Return False
                    End If
                End If
            Next


            For i = 0 To 3
                If Not CheckTextNumber(g_strResultTitle(35 + i), ProductModel.Limits(i), 99999, -99999) Then
                    Return False
                End If

                If i Mod 2 = 1 Then
                    If ProductModel.Limits(i) > ProductModel.Limits(i - 1) Then
                        log.WriteLogError("输入参数检查", "[" & g_strResultTitle(35 + i) & "]不能大于[" & g_strResultTitle(35 + i - 1) & "]！")
                        Return False
                    End If
                End If

            Next


            For i = 4 To ProductModel.Limits.Count - 1
                If i Mod 2 = 0 Then
                    If Not CheckTextNumber(g_strResultTitle(35 + i), ProductModel.Limits(i), 99999, 0) Then
                        Return False
                    End If
                Else
                    If Not CheckTextNumber(g_strResultTitle(35 + i), ProductModel.Limits(i), 0, -99999) Then
                        Return False
                    End If
                End If
            Next

            If Not CheckTextNumber("小漏偏差值", ProductModel.SOffsetVal, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("大漏偏差值", ProductModel.LOffsetVal, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("小漏线性系数K", ProductModel.SLinearK, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("大漏线性系数K", ProductModel.LLinearK, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("小漏线性系数B", ProductModel.SLinearB, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("大漏线性系数B", ProductModel.LLinearB, 99999, -99999) Then
                Return False
            End If



            If Not CheckTextNumber("补偿值上限", ProductModel.RevUl, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("当前补偿值", ProductModel.RevVal, 99999, -99999) Then
                Return False
            End If

            If ProductModel.RevEn Then
                If Not CheckTextNumber("补偿系数", ProductModel.RevWeight, 0.9, 0.1) Then
                    Return False
                End If
            End If

            If Not CheckTextNumber("补偿有效结果上限", ProductModel.RevNg, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("Fitting系数A", ProductModel.FittingA, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("Fitting系数B", ProductModel.FittingB, 99999, -99999) Then
                Return False
            End If

            If Not CheckTextNumber("Fitting系数C", ProductModel.FittingC, 0, -99999) Then
                Return False
            End If


            If Not CheckTextNumber("被测物容积", ProductModel.WorkVal, 99999, 1) Then
                Return False
            End If

            If Not CheckTextNumber("基准物容积", ProductModel.MasterVal, 99999, 1) Then
                Return False
            End If

            If Not CheckTextNumber("标准漏孔值", ProductModel.StandLeak, 99999, 0) Then
                Return False
            End If

            Return True
        End Function


        '=====================================================================
        '函 数 名: FillAddModel
        '参    数: 
        '返 回 值:
        '功能描述: 读取设备数据填充ProductModel
        '=====================================================================
        Private Sub FillAddModel()
            IsAdd = True

            Dim udtProduct = New ProductModel()
            IdList.Clear()
            Id = SystemData.idWorker.nextId()
            IdList.Add(Id)

            udtProduct.GroupId = uLPU.OUT_ReadUint16(1, ADDR_OUT_GROUP)

            For i = 0 To udtProduct.Times.Count - 1
                udtProduct.Times(i) = CStr(uLPU.OUT_ReadSingle(1, ADDR_OUT_TIME_DELAY1 + i * 2)) '环节时间
            Next i

            For i = 0 To udtProduct.Limits.Count - 1
                udtProduct.Limits(i) = CStr(uLPU.OUT_ReadSingle(1, ADDR_OUT_LEAD_P_UP + i * 2)) '环节时间
            Next i

            udtProduct.GpUnitIndex = uLPU.OUT_ReadUint16(1, ADDR_OUT_GP_UNIT)
            udtProduct.LeakUnitIndex = uLPU.OUT_ReadUint16(1, ADDR_OUT_SDET_UNIT)


            udtProduct.SOffsetEn = uLPU.OUT_ReadUint16(1, ADDR_OUT_SDET_OFFICE_EN) <> 0
            udtProduct.SOffsetVal = uLPU.OUT_ReadSingle(1, ADDR_OUT_SDET_OFFICE_VALUE)
            udtProduct.SLinearEn = uLPU.OUT_ReadInt16(1, ADDR_OUT_SDET_LINEAR_EN) <> 0
            udtProduct.SLinearK = uLPU.OUT_ReadInt16(1, ADDR_OUT_SDET_LINEAR_K) <> 0
            udtProduct.SLinearB = uLPU.OUT_ReadInt16(1, ADDR_OUT_SDET_LINEAR_B) <> 0

            udtProduct.LOffsetEn = uLPU.OUT_ReadUint16(1, ADDR_OUT_LDET_OFFICE_EN) <> 0
            udtProduct.LOffsetVal = uLPU.OUT_ReadSingle(1, ADDR_OUT_LDET_OFFICE_VALUE)
            udtProduct.LLinearEn = uLPU.OUT_ReadInt16(1, ADDR_OUT_LDET_LINEAR_EN) <> 0
            udtProduct.LLinearK = uLPU.OUT_ReadInt16(1, ADDR_OUT_LDET_LINEAR_K) <> 0
            udtProduct.LLinearB = uLPU.OUT_ReadInt16(1, ADDR_OUT_LDET_LINEAR_B) <> 0

            udtProduct.RevEn = uLPU.OUT_ReadUint16(1, ADDR_OUT_REV_MODE) <> 0
            udtProduct.RevSave = uLPU.OUT_ReadUint16(1, ADDR_OUT_REV_SAVE) <> 0
            udtProduct.RevUl = CStr(uLPU.OUT_ReadSingle(1, ADDR_OUT_REV_LIMIT))
            udtProduct.RevVal = CStr(uLPU.OUT_ReadSingle(1, ADDR_OUT_REV_DATA))
            udtProduct.RevWeight = CStr(uLPU.OUT_ReadSingle(1, ADDR_OUT_REV_WEIGHT))
            udtProduct.RevNg = CStr(uLPU.OUT_ReadSingle(1, ADDR_OUT_REV_NG))

            udtProduct.FittingMode = uLPU.OUT_ReadUint16(1, ADDR_OUT_FITTING_EN)
            udtProduct.FittingA = uLPU.OUT_ReadSingle(1, ADDR_OUT_FITTING_A)
            udtProduct.FittingB = uLPU.OUT_ReadSingle(1, ADDR_OUT_FITTING_B)
            udtProduct.FittingC = uLPU.OUT_ReadSingle(1, ADDR_OUT_FITTING_C)

            udtProduct.WorkVal = uLPU.OUT_ReadSingle(1, ADDR_OUT_WORK_VOL)
            udtProduct.MasterVal = uLPU.OUT_ReadSingle(1, ADDR_OUT_MASTER_VOL)
            udtProduct.StandLeak = CStr(uLPU.OUT_ReadSingle(1, ADDR_OUT_LEAK_VALUE))
            udtProduct.RevCalc = uLPU.OUT_ReadUint16(1, ADDR_OUT_NEGATIVE_CALCULATION) <> 0

            GpUnit = g_strGPUnit(udtProduct.GpUnitIndex)
            If udtProduct.LeakUnitIndex = 1 Then
                LeakUnit = GpUnit
            Else
                LeakUnit = g_strLeakUnit(udtProduct.LeakUnitIndex)
            End If
            ProductModel = udtProduct

        End Sub

        '=====================================================================
        '函 数 名: FillEditModel
        '参    数: strId 产品Id号
        '返 回 值:
        '功能描述: 读取数据库数据填充ProductModel
        '=====================================================================
        Private Sub FillEditModel(ByVal strId As String)
            ProductModel = User_ReadProduct(strId)
            GpUnit = g_strGPUnit(ProductModel.GpUnitIndex)
            If ProductModel.LeakUnitIndex = 1 Then
                LeakUnit = GpUnit
            Else
                LeakUnit = g_strLeakUnit(ProductModel.LeakUnitIndex)
            End If
        End Sub

        '=====================================================================
        '函 数 名: Apply
        '参    数: 
        '返 回 值:
        '功能描述: 数据存库
        '=====================================================================
        Private Sub Apply()
            ProductModel.Id = Id
            If CheckInput() Then
                ProductModel.Name = IIf(ProductModel.Name = "", "未命名", ProductModel.Name)
                ProductModel.Model = IIf(ProductModel.Model = "", "空", ProductModel.Model)
                Dim result As Boolean = User_WriteProduct(ProductModel, Reason)
                If result Then
                    Reason = ""
                    MsgBox("保存成功", MsgBoxStyle.OkOnly, "提示")
                End If
            End If
        End Sub

        Public Property Id As String
            Get
                Return _id
            End Get
            Set
                If _id <> Value Then
                    FillEditModel(Value)
                End If
                _id = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property ProductModel As ProductModel
            Get
                Return _productModel
            End Get
            Set
                _productModel = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property IdList As ObservableCollection(Of String)
            Get
                Return _idList
            End Get
            Set
                _idList = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property GpUnit As String
            Get
                Return _gpUnit
            End Get
            Set(value As String)
                _gpUnit = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property LeakUnit As String
            Get
                Return _leakUnit
            End Get
            Set(value As String)
                _leakUnit = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property GpUnitList As ObservableCollection(Of String)
            Get
                Return _gpUnitList
            End Get
            Set(value As ObservableCollection(Of String))
                _gpUnitList = value
            End Set
        End Property

        Public Property LeakUnitList As ObservableCollection(Of String)
            Get
                Return _leakUnitList
            End Get
            Set(value As ObservableCollection(Of String))
                _leakUnitList = value
            End Set
        End Property

        Public Property AddCommand As RelayCommand
            Get
                Return _addCommand
            End Get
            Set(value As RelayCommand)
                _addCommand = value
            End Set
        End Property

        Public Property EditCommand As RelayCommand
            Get
                Return _editCommand
            End Get
            Set(value As RelayCommand)
                _editCommand = value
            End Set
        End Property

        Public Property ApplyCommand As RelayCommand
            Get
                Return _applyCommand
            End Get
            Set(value As RelayCommand)
                _applyCommand = value
            End Set
        End Property

        Public Property CancelCommand As RelayCommand
            Get
                Return _cancelCommand
            End Get
            Set(value As RelayCommand)
                _cancelCommand = value
            End Set
        End Property

        Public Property Reason As String
            Get
                Return _reason
            End Get
            Set(value As String)
                _reason = value
                EnableSave = Not String.IsNullOrWhiteSpace(_reason)
                RaisePropertyChanged()
            End Set
        End Property

        Public Property EnableSave As Boolean
            Get
                Return _enableSave
            End Get
            Set
                _enableSave = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property FittingModeList As ObservableCollection(Of String)
            Get
                Return _fittingModeList
            End Get
            Set
                _fittingModeList = Value
            End Set
        End Property

        Public Property IsAdd As Boolean
            Get
                Return _isAdd
            End Get
            Set(value As Boolean)
                _isAdd = value
                RaisePropertyChanged("IsAdd")
                RaisePropertyChanged("IsUpdate")
            End Set
        End Property

        Public Property IsUpdate As Boolean
            Get
                Return Not _isAdd
            End Get
            Set(value As Boolean)
                IsAdd = Not value
            End Set
        End Property
    End Class
End Namespace